﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ReCaptcha.cs" company="Open Source">
//   MIT License
// </copyright>
// <summary>
//   The re captcha.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Contour.Contrib.FieldTypes
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI.WebControls;
    using System.Web.Script.Serialization;

    using Recaptcha;

    using Umbraco.Forms.Core;
    using Umbraco.Forms.Core.Attributes;

    using Contour.Contrib.Controls;

    /// <summary>
    /// The re captcha.
    /// </summary>
    public sealed class ReCaptcha : FieldType
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ReCaptcha"/> class.
        /// </summary>
        public ReCaptcha()
        {
            this.Id = new Guid("F8C089B9-7D8F-47B0-A594-9FD9903FBF42");
            this.Name = "ReCaptcha";
            this.Description = "Renders a ReCaptcha";

            // FieldType
            this.Icon = "textfield.png";
                
                // needs to be in the \umbraco\plugins\umbracoContour\images\fieldtypes directory
            this.DataType = FieldDataType.String;
            this.Recaptcha = new ReCaptchaCustomControl();
            this.Value = new List<object>();
        }

        #endregion        
        
        #region Properties

        /// <summary>
        /// Gets or sets recaptcha.
        /// </summary>
        public ReCaptchaCustomControl Recaptcha { get; set; }

        /// <summary>
        /// Gets or sets value.
        /// </summary>
        public List<object> Value { get; set; }

        /// <summary>
        /// Gets or sets CustomTranslations.
        /// </summary>
        [Setting("CustomTranslations", prevalues = "", description = "Custom translations", 
            control = "Umbraco.Forms.Core.FieldSetting.TextField")]
        public string CustomTranslations { get; set; }

        JavaScriptSerializer serializer;

        /// <summary>
        /// Gets or sets Editor.
        /// </summary>
        public override WebControl Editor
        {
            get
            {
                this.Recaptcha.PrivateKey = this.PrivateKey;
                this.Recaptcha.PublicKey = this.PublicKey;
                this.Recaptcha.Theme = this.Theme;
                this.Recaptcha.EnableTheming = false;

                if (this.Theme == "custom")
                {
                    this.Recaptcha.EnableTheming = true;
                    this.Recaptcha.CustomThemeWidget = this.CustomThemeWidget;
                }

                // if (AssociatedField.Mandatory == false)
                // {
                // Recaptcha.SkipRecaptcha = true;
                // HideField = true;
                // }

                this.Recaptcha.Language = this.Language;

                //String for custom translations: "instructions_visual":"Scrivi le due parole:","instructions_audio":"Trascrivi ci\u00f2 che senti:","play_again":"Riascolta la traccia audio","cant_hear_this":"Scarica la traccia in formato MP3","visual_challenge":"Modalit\u00e0 visiva","audio_challenge":"Modalit\u00e0 auditiva","refresh_btn":"Chiedi due nuove parole","help_btn":"Aiuto","incorrect_try_again":"Scorretto. Riprova."

                if (!string.IsNullOrEmpty(this.CustomTranslations))
                {
                    serializer = new JavaScriptSerializer();
                    Dictionary<string, string> DictionryCustomTranslations = serializer.Deserialize<Dictionary<string, string>>("{" + this.CustomTranslations + "}");
                    if (DictionryCustomTranslations.Count > 0)
                        this.Recaptcha.CustomTranslations = DictionryCustomTranslations;
                }
                return this.Recaptcha;
            }

            set
            {
                base.Editor = value;
            }
        }

        /// <summary>
        /// Gets or sets Language.
        /// </summary>
        [Setting("Language", prevalues = "en,nl,fr,de,pt,ru,es,tr", description = "Language", 
            control = "Umbraco.Forms.Core.FieldSetting.Dropdownlist")]
        public string Language { get; set; }

        /// <summary>
        /// Gets or sets PublicKey.
        /// </summary>
        [Setting("PublicKey", prevalues = "", description = "ReCaptcha pPublic key",
            control = "Umbraco.Forms.Core.FieldSetting.TextField")]
        public string PublicKey { get; set; }

        /// <summary>
        /// Gets or sets PrivateKey.
        /// </summary>
        [Setting("PrivateKey", prevalues = "", description = "ReCaptcha private key", 
            control = "Umbraco.Forms.Core.FieldSetting.TextField")]
        public string PrivateKey { get; set; }

        /// <summary>
        /// Gets a value indicating whether SupportsPrevalues.
        /// </summary>
        public override bool SupportsPrevalues
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// Gets a value indicating whether SupportsRegex.
        /// </summary>
        public override bool SupportsRegex
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// Gets or sets Theme.
        /// </summary>
        [Setting("Theme", prevalues = "red,white,blackglass,clean,custom", description = "ReCaptcha theme", 
            control = "Umbraco.Forms.Core.FieldSetting.Dropdownlist")]
        public string Theme { get; set; }

        /// <summary>
        /// Gets or sets CustomThemeWidget.
        /// </summary>
        [Setting("CustomThemeWidget", prevalues = "", description = "Custom theme ElementId",
            control = "Umbraco.Forms.Core.FieldSetting.TextField")]
        public string CustomThemeWidget { get; set; }
        
        /// <summary>
        /// Gets or sets Values.
        /// </summary>
        public override List<object> Values
        {
            get
            {
                this.Value.Clear();
                this.Value.Add(string.Empty);
                return this.Value;
            }

            set
            {
                this.Value = value;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// The render preview.
        /// </summary>
        /// <returns>
        /// The preview.
        /// </returns>
        public override string RenderPreview()
        {
            return "<img src=\"/umbraco/plugins/umbracoContour/images/recaptcha-example.gif\" alt=\"recaptcha\" />";
        }

        /// <summary>
        /// The render preview with prevalues.
        /// </summary>
        /// <param name="prevalues">
        /// The prevalues.
        /// </param>
        /// <returns>
        /// The preview with prevalues
        /// </returns>
        public override string RenderPreviewWithPrevalues(List<object> prevalues)
        {
            return this.RenderPreview();
        }

        #endregion
    }
}